import scrapy
import re
from ..items import DoubanproItem


class DoubanSpider(scrapy.Spider):
    name = "douban"
    start_urls = ["https://movie.douban.com/chart"]

    # def parse(self, response):
    #     # 解析数据
    #     table_list = response.xpath('//*[@id="content"]/div/div[1]/div/div/table')
    #     all_data = []
    #     for table in table_list:
    #         movie_title = str(table.xpath('./tr/td[2]/div/a/text()')[0])
    #         # print("titile", type(movie_title))
    #         movie_title = movie_title.replace(" ", "")
    #         movie_title = movie_title.replace("\n", "")
    #         movie_title = movie_title.replace('/', "")
    #         movie_src = str(table.xpath('./tr/td[1]/a/@href')[0])
    #         # print("src", type(movie_src))
    #         # print(movie_title+":"+movie_src)
    #         dic = {
    #             'title': movie_title,
    #             'address': movie_src
    #         }
    #         all_data.append(dic)
    #
    #     return  all_data

    def parse(self, response):
        # 解析数据
        table_list = response.xpath('//*[@id="content"]/div/div[1]/div/div/table')
        all_data = []
        for table in table_list:
            movie_title = str(table.xpath('./tr/td[2]/div/a/text()')[0])
            # print("titile", type(movie_title))
            movie_title = movie_title.replace(" ", "")
            movie_title = movie_title.replace("\n", "")
            movie_title = movie_title.replace('/', "")
            movie_src = str(table.xpath('./tr/td[1]/a/@href')[0])

            # 将数据分装到item
            item = DoubanproItem()
            item['title'] = movie_title
            item['address'] = movie_src

            # 将item提交到管道
            yield item
